﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eis51.CampanyCMS.Model.DownLoadMgnt;
using Eis51.CampanyCMS.Business.DownLoadMgnt;
using Eis51.CampanyCMS.Model;
using System.Collections;

namespace Eis51.Web.Admin.DownLoadFileMgnt
{
    public partial class edit_type : BasePage
    {
        private int TypeId
        {
            get
            {
                if (Request.QueryString["typeid"] == null)
                {
                    return 0;
                }
                else
                {
                    return int.Parse(Request.QueryString["typeid"]);
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
            }

        }

        private void InitData()
        {
            DownloadFileType obj = DownloadFileTypeBLL.GetInstance().Get(new DownloadFileType.Key((TypeId)));
            if (obj != null)
            {
                ddlPosition.SelectedValue = obj.TypeName;
                txtShowDescr.Text = obj.Desc;
            }
        }

        private bool isExist(DownloadFileType objExist)
        {
            IList list = DownloadFileTypeBLL.GetInstance().Select();
            DownloadFileType model = null;
            foreach (var obj in list)
            {
                model = (DownloadFileType)obj;
                if (null == objExist)//新增
                    if (ddlPosition.SelectedValue.Equals(model.TypeName))
                    {
                        return true;
                    }
                if (null != objExist &&  model.DownloadFileTypeId == objExist.DownloadFileTypeId)//等于当前编辑对象
                {
                    continue;
                }
                if (null != objExist && ddlPosition.SelectedValue.Equals(model.TypeName))//修改时已存在
                {
                    return true;
                }

            }

            return false;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {            
            
            DownloadFileType obj = DownloadFileTypeBLL.GetInstance().Get(new DownloadFileType.Key((TypeId)));
            if (isExist(obj))
            {
                Response.Write("<script>alert('请选位置模块已存在，请重新选择');</script>");
                return;
            }
            if (obj == null)
            {                
                obj = new DownloadFileType();
            }
            obj.TypeName = ddlPosition.SelectedValue;
            obj.Desc = txtShowDescr.Text;
            obj.Createby = AppContext.CurrentAdministrator.UserName;
            obj.Createdate = DateTime.Now;
            DownloadFileTypeBLL.GetInstance().Submit(obj);

            btnReturn_Click(null, null);

        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.QueryString["r"]);
        }
    }
}
